<?php
//普通类
// cllass Db {
//     public  function __construct(){

//     }
// }
// $db = new Db();
// $db1 = new Db();
//......

//单列模式类(只能拥有一个实列)
//
//error_reporting(7);
class Db {
    static private $_instance;
    static private $_connectSource;
    private $_dbConfig = array(
        'host' =>'127.0.0.1',
        'user' => 'root',
        'password' => '',
        'database' =>'test'
    );

    private  function __construct(){
    }

    static public function getInstance(){
            if(!(self::$_instance instanceof self)){
                   self::$_instance = new self();
            }
            return self::$_instance;
    }
    public function connect(){
          if(!self::$_connectSource){
                self::$_connectSource = mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);

                if(!self::$_connectSource){
                    //抛异常
                    //客户端接收异常方法
                    //try{
                    //      $connect = Db::getInstance()->connect();
                    //}catch(Exception $e){
                    //      //$e->getMassage();
                    //      return Response::show(403,'数据库连接失败');
                    //}
                    throw new Exception('mysql connect error'.mysql_error());
                    // die('mysql connect error'.mysql_error());
                }

                mysql_select_db($this->_dbConfig['database'], self::$_connectSource);
                mysql_query('set names UTF8', self::$_connectSource);
            }
            return self::$_connectSource;
    }
}


//以下演示代码

// $connect = Db::getInstance()->connect();
// $sql ='select * from wp_posts where id>=172 && id <=232 group by id DESC';
// $row = mysql_query($sql,$connect);
// $i=0;
// while ( ($nahll = mysql_fetch_assoc($row)) != false) {
//     echo '<div style="margin:50px auto 0px;width:80%;">';
//     echo '<h2 style=""><a href="#" style="text-decoration-style=none;">'.$nahll['post_title'].'</a></h2>';
//     echo '<p style="margin:5px auto 0px;">'.$nahll['post_date'].'</p>';
//     echo '<p style="heigth:400px;text-align:justify; text-justify:inter-ideograph;">'.$nahll['post_content'].'</p>';
//     echo '</div>';
//     echo $nahll['ID'];
//     $i=$i+1;
// }




